package com.yuexunxinxi.carefree.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者: 李咨辰
 * 文件名: AlibabaOSS.java
 * 路径: com.yuexunxinxi.carefree.utils
 * 时间: 2024-12-13 11:57
 * 版本: 1.0
 * 描述: TODO
 **/
@Component
public class AlibabaOSS {

    private static String ACCESS_KEY_ID;
    private static String SECRET_ACCESS_KEY;
    private static String ENDPOINT;
    private static String BUCKET_NAME;
    private static String REGION;


    @Value("${alibaba.accessKeyId}")
    private String GET_ACCESS_KEY_ID;
    @Value("${alibaba.secretAccessKey}")
    private String GET_SECRET_ACCESS_KEY;
    @Value("${alibaba.oss.endpoint}")
    private String GET_ENDPOINT;
    @Value("${alibaba.oss.bucketName}")
    private String GET_BUCKET_NAME;
    @Value("${alibaba.oss.region}")
    private String GET_REGION;

    @PostConstruct
    public void init() {
        ENDPOINT = GET_ENDPOINT;
        SECRET_ACCESS_KEY = GET_SECRET_ACCESS_KEY;
        ACCESS_KEY_ID = GET_ACCESS_KEY_ID;
        BUCKET_NAME = GET_BUCKET_NAME;
        REGION = GET_REGION;
    }

    /**
     * 文件上传
     *
     * @param file 文件
     * @return 文件路径
     */
    public static String upload(MultipartFile file) {
        File document = new File(System.getProperty("java.io.tmpdir") + "/" + file.getOriginalFilename());
        try {
            file.transferTo(document);// 将MultipartFile内容写入文件
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return AlibabaOSS.upload(document);
    }

    public static String upload(File file) {
        String path = "upload/" + new SimpleDateFormat("yyyy/MM/dd/HH").format(new Date());//路径
        long fileName = SnowFlakeUtil.getFlowIdInstance().nextId();//文件名
        String substring = file.getName().substring(file.getName().lastIndexOf('.'));//后缀
        String objectName = path + "/" + fileName + substring;
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, file);
        ossClient.putObject(putObjectRequest);
        return "https://oss.yuexunxinxi.com/" + objectName;
    }

    /**
     * 文件删除
     *
     * @param path 填写文件完整路径。文件完整路径中不能包含Bucket名称。
     * @return 成功返回true
     */
    public static boolean delFile(String path) {
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
        boolean lag = true;
        try {
            // 删除文件或目录。如果要删除目录，目录必须为空。
            String pathSplit = path.split("//")[1];
            int slashIndex = pathSplit.indexOf("/");
            ossClient.deleteObject(BUCKET_NAME, pathSplit.substring(slashIndex + 1));
        } catch (OSSException oe) {
            lag = false;
        } finally {
            ossClient.shutdown();
        }
        return lag;
    }


}
